package com.wuxinwudai.android.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.wuxinwudai.android.app.AppConfig;
import com.wuxinwudai.android.app.BaseApplication;

import java.util.Set;


/**
 * Created by 吾心无待 on 2015/11/6.
 */
public class SharedPreferencesUtils {
    private SharedPreferences mSharedPreferences;
    private static SharedPreferencesUtils mInstance;

    private SharedPreferencesUtils() throws Exception{
        Context ctx = BaseApplication.getContext();
        mSharedPreferences = ctx.getSharedPreferences(ctx.getPackageName()+"_SHARED", Context.MODE_PRIVATE);
    }

    /**
     * 获取默认的 SharedPreferences 对象
     * @return
     */
    public SharedPreferences getSharedPreferences(){
        return mSharedPreferences;
    }
    /**
     * 单例获取 SharedPreferencesUtils 对象
     * @return
     */
    public static SharedPreferencesUtils getInstance() throws Exception{
        if (mInstance == null){
            mInstance = new SharedPreferencesUtils();
        }
        return mInstance;
    }

    /**
     * 获取 浮点数 值
     * @param key SharedPreferences 查询关键字
     * @param defaultValue 默认值
     * @return
     * @throws Exception
     */
    public static float getFloot(final String key,final float defaultValue) throws Exception{
        return SharedPreferencesUtils.getInstance().getSharedPreferences().getFloat(key,defaultValue);
    }

    /**
     * 存放 浮点数 值
     * @param key SharedPreferences 查询关键字
     * @param value 配置值
     * @return
     */
    public static boolean putFloot(final String key,final float value){
        try {
            final SharedPreferences.Editor editor = SharedPreferencesUtils.getInstance().getSharedPreferences().edit();
            editor.putFloat(key,value);
            return editor.commit();
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 获取 整数 值
     * @param key SharedPreferences 查询关键字
     * @param defaultValue 默认值
     * @return
     * @throws Exception
     */
    public static int getDouble(final String key,final int defaultValue) throws Exception{
        return SharedPreferencesUtils.getInstance().getSharedPreferences().getInt(key, defaultValue);
    }

    /**
     * 存放 整数 值
     * @param key SharedPreferences 查询关键字
     * @param value 配置值
     * @return
     */
    public static boolean putInt(final String key,final int value){
        try {
            final SharedPreferences.Editor editor = SharedPreferencesUtils.getInstance().getSharedPreferences().edit();
            editor.putInt(key, value);
            return editor.commit();
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 获取 字符串 值
     * @param key SharedPreferences 查询关键字
     * @param defaultValue 默认值
     * @return
     * @throws Exception
     */
    public static String getString(final String key,final String defaultValue) throws Exception{
        return SharedPreferencesUtils.getInstance().getSharedPreferences().getString(key, defaultValue);
    }

    /**
     * 存放 浮点数 值
     * @param key SharedPreferences 查询关键字
     * @param value 配置值
     * @return
     */
    public static boolean putString(final String key,final String value){
        try {
            final SharedPreferences.Editor editor = SharedPreferencesUtils.getInstance().getSharedPreferences().edit();
            editor.putString(key, value);
            return editor.commit();
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 获取 长整形 值
     * @param key SharedPreferences 查询关键字
     * @param defaultValue 默认值
     * @return
     * @throws Exception
     */
    public static long getLong(final String key,final long defaultValue) throws Exception{
        return SharedPreferencesUtils.getInstance().getSharedPreferences().getLong(key, defaultValue);
    }

    /**
     * 存放 长整形 值
     * @param key SharedPreferences 查询关键字
     * @param value 配置值
     * @return
     */
    public static boolean putLong(final String key,final long value){
        try {
            final SharedPreferences.Editor editor = SharedPreferencesUtils.getInstance().getSharedPreferences().edit();
            editor.putLong(key, value);
            return editor.commit();
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 获取布尔值
     * @param key SharedPreferences 查询关键字
     * @param defaultValue 默认值
     * @return
     * @throws Exception
     */
    public static boolean getBoolean(final String key,final boolean defaultValue) throws Exception{
        return SharedPreferencesUtils.getInstance().getSharedPreferences().getBoolean(key, defaultValue);
    }



    /**
     * 存放布尔值
     * @param key SharedPreferences 查询关键字
     * @param value 配置值
     * @return
     */
    public static boolean putBoolean(final String key,final boolean value){
        try {
            final SharedPreferences.Editor editor = SharedPreferencesUtils.getInstance().getSharedPreferences().edit();
            editor.putBoolean(key, value);
            return editor.commit();
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 获取字符串集值
     * @param key SharedPreferences 查询关键字
     * @param defaultValue 默认值
     * @return
     * @throws Exception
     */
    public static Set<String> getStringSet(final String key,final Set<String> defaultValue) throws Exception{
        return SharedPreferencesUtils.getInstance().getSharedPreferences().getStringSet(key, defaultValue);
    }

    /**
     * 存放字符串集值
     * @param key SharedPreferences 查询关键字
     * @param value 配置值
     * @return
     */
    public static boolean putStringSet(final String key,final Set<String> value){
        try {
            final SharedPreferences.Editor editor = SharedPreferencesUtils.getInstance().getSharedPreferences().edit();
            editor.putStringSet(key, value);
            return editor.commit();
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }
}
